package com.hxxy.wyp.petsLover.common.rest.base.fronted;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.gson.Gson;
import com.hxxy.wyp.petsLover.RestBase;
import com.hxxy.wyp.petsLover.common.entities.Good;
import com.hxxy.wyp.petsLover.common.entities.Orders;
import com.hxxy.wyp.petsLover.common.entities.ShopCart;
import com.hxxy.wyp.petsLover.common.entities.vo.req.EvalutionReq;
import com.hxxy.wyp.petsLover.common.entities.vo.req.PayVo;
import com.hxxy.wyp.petsLover.common.entities.vo.resp.OrderEvolutionVo;
import com.hxxy.wyp.petsLover.common.entities.vo.resp.OrdersVo;
import com.hxxy.wyp.petsLover.common.services.base.backend.GoodService;
import com.hxxy.wyp.petsLover.common.services.base.backend.OrderService;
import com.hxxy.wyp.petsLover.common.services.base.fronted.ShopCartService;
import com.hxxy.wyp.petsLover.config.AlipayTemplate;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName OrderFrontRest
 * @Description TODO
 * @Author wyp
 * @Date 2023/2/15 10:00
 * @Version 1.0
 */
@RestController
@ResponseBody
@RequestMapping("/orderFront")
public class OrderFrontRest extends RestBase {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ShopCartService shopCartService;

    @Autowired
    private GoodService goodService;

    @Autowired
    private AlipayTemplate alipayTemplate;

    @SaCheckLogin
    @RequestMapping("/selectOrderListByUser")
    public Object selectOrderListByUser() {
        List<OrdersVo> list = orderService.selectOrderListByUser();

        return ajaxSuccess(list, "200", "查询成功!");
    }


    @SaCheckLogin
    @RequestMapping("/selectOrderInfoMore")
    public Object selectOrderInfoMore(@RequestParam("id") String id) {
        OrdersVo ordersVo = orderService.selectOrderInfoMore(id);
        return ajaxSuccess(ordersVo, "200");
    }


    @SaCheckLogin
    @RequestMapping("/evaluatedCommodity")
    public Object evaluatedCommodity(@RequestBody EvalutionReq evalutionReq) {
        orderService.evaluatedCommodity(evalutionReq);
        return ajaxSuccess("200", "评价成功!");
    }


    @SaCheckLogin
    @RequestMapping("/selectAllOrderEvolutions")
    public Object selectAllOrderEvolutions() {
        OrderEvolutionVo evolutionVo = orderService.selectAllOrderEvolutions();
        return ajaxSuccess(evolutionVo, "200", "查询成功!");
    }

    /**
     * 支付业务
     */
    @SaCheckLogin
    @RequestMapping(value = "/payOrder")
    public Object payOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {
        PayVo payVo = orderService.getOrderPay(orderSn);
        String pay = alipayTemplate.pay(payVo);
        Gson gson = new Gson();
        String payJson = gson.toJson(pay, String.class);
        return ajaxSuccess(payJson, "200", "调用接口返回json格式的html");
    }

    /**
     * 支付成功!修改订单状态
     */
    @SaCheckLogin
    @RequestMapping("/updateOrderStatus")
    public Object updateOrderStatus(@RequestParam("orderSn") String orderSn) {
        LambdaQueryWrapper<Orders> eq = new LambdaQueryWrapper<>(Orders.class).eq(Orders::getOrderId, Long.parseLong(orderSn));
        //修改订单状态
        Orders orders = orderService.getOne(eq);
        orders.setOrderStatus("1");
        orders.setOrderPaymentTime(LocalDateTime.now());
        boolean b = orderService.updateById(orders);
        //增加已售卖数量
        List<String> shopCartIdList = Arrays.stream(orders.getShopCartId().split(",")).collect(Collectors.toList());
        shopCartIdList.forEach(shopCartId -> {
            ShopCart shopCart = shopCartService.getById(shopCartId);
            long goodId = shopCart.getGoodId();
            Good good = goodService.getById(goodId);
            good.setSaleNumber(good.getSaleNumber()+shopCart.getCommodityNumber());
            good.setInventory(good.getInventory()-shopCart.getCommodityNumber());
            goodService.updateById(good);
        });
        if (b) {
            return ajaxSuccess("200", "修改订单状态成功!");
        }
        return ajaxError("修改订单状态失败!");
    }


    @SaCheckLogin
    @RequestMapping("/deleteOrder")
    public Object deleteOrder(@RequestParam("id") String id) {
        boolean b = orderService.removeById(id);
        if (b) {
            return ajaxSuccess("200", "删除订单成功!");
        }
        return ajaxError("删除失败");
    }

}
